/*
 * Created on Feb 14, 2009
 *
 * TODO
 */
package jacky.lanlan.song.extension.vraptor2;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;

import org.vraptor.plugin.niceurls.NiceURLFilter;

/**
 * <i style="color:red">默认的 {@code NiceURLFilter} 会拦截所有请求，包括对js和css文件的请求。</i>
 * <p>
 * 这个 Filter 对 {@code NiceURLFilter} 进行了增强，可以配置初始参数 exclude-url-pattern (正则表达式)， 任何匹配该 Pattern 的 URL
 * 请求都将被这个 Filter 忽略而直接发往下一个过滤器。
 * 
 * @author Jacky.Song
 */
public class EnhancedURLFilter extends NiceURLFilter {

	private static String excludeURLPattern; // 不需要经过这个过滤器的请求URL Pattern

	@Override
	public void init(FilterConfig fc) throws ServletException {
		super.init(fc);
		excludeURLPattern = fc.getInitParameter("exclude-url-pattern");
	}

	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {

		HttpServletRequest http = (HttpServletRequest) servletRequest;

		if (excludeURLPattern != null && http.getRequestURL().toString().matches(excludeURLPattern))
			filterChain.doFilter(servletRequest, servletResponse);
		else
			super.doFilter(servletRequest, servletResponse, filterChain);
	}

}
